
package cs2114.antiFingerMines;

// -------------------------------------------------------------------------
/**
 *  This class along with the four other MineDistance clasess allows us to
 *  calculate the distance from the finger and all the mines 4 to 5
 *  times faster by extending Thread
 *
 *  Thread allows you run multiple sets code of at one time. In our case
 *  since we have 5 threads. We can do all 5 threads at the same time.
 *  So we are calculating the distance from our finger to five mines at
 *  one time as opposed to one mine at one time. There is a lot less lag.
 *
 *  @author William-Laptop
 *  @version Apr 20, 2012
 */
public class MineDistanceOneb extends Thread
{

    // Initializes the finger, minefield, and closest to be set in the
    // constructor
	Location finger;
	Mine[] field;
	float closest;

	// ----------------------------------------------------------
	/**
	 * Create a new MineDistanceOne object.
	 * @param loc the fingers current location
	 * @param mines the field of mines
	 */
	public MineDistanceOneb(Location loc, Mine[] mines)
	{
		finger = loc;
		field = mines;
		// Sets closest initially to be 99999 so that the actual closest
		// distance will be for sure less than it.
		closest = 99999;
	}

	/**
	 * This thread only runs on the first fifth of the mine field and also
	 * sets the smallest distance between the finger and the mine to closest
	 */
	public void run()
	{
		for (int i = 0; i < field.length/5; i++)
        {
            if (field[i].traversableDistanceBetween(finger) <= closest)
            {
                closest = field[i].traversableDistanceBetween(finger);
            }
        }

	}

	/**
	 * This method returns closest so that it can be compared to the closest
	 * values returned from the other threads.
	 * @return float is the closest mine to the finger in the first fifth
	 * of the minefield
	 */
	public float getClosest()
	{
		return closest;
	}

}
